<?php
use think\Db;
/**
 * 生成操作按钮
 * @param array $operate 操作按钮数组
 */
function showOperate($operate = [])
{
    if(empty($operate)){
        return '';
    }

    $option = '';
    foreach($operate as $key=>$vo){
        if(authCheck($vo['auth'])){
            $option .= ' <a href="' . $vo['href'] . '"><button type="button" class="btn btn-' . $vo['btnStyle'] . ' btn-sm">'.
                '<i class="' . $vo['icon'] . '"></i> ' . $key . '</button></a>';
        }
    }

    return $option;
}

// 查询是否关注
function isAttent($id, $attentId)
{
    $attent = Db::table(['wb_attent' => 'a'])
        ->field(['a.user_id', 'a.attent_id'])
        ->where('a.user_id=' . $id)
        ->where(' a.attent_id=' . $attentId)
        ->find();
//            halt($attent);
    $fans = Db::table(['wb_fans' => 'f'])
        ->field(['f.user_id', 'f.fans_id'])
        ->where('f.user_id=' . $attentId)
        ->where('fans_id=' . $id)
        ->find();
//            halt($fans);
    $both = Db::table(['wb_attent' => 'a', 'wb_fans' => 'f'])
        ->where('a.user_id=f.user_id and a.attent_id=f.fans_id')
        ->find();
//            halt($both);
    if (null != $attent) {
        if (null != $fans && null != $both) {
            $attentStatus = 3;
        } else {
            $attentStatus = 2;
        }
    } else {
        $attentStatus = 1;
    }
    return $attentStatus;
}
/**
 * 将字符解析成数组
 * @param $str
 */
function parseParams($str)
{
    $arrParams = [];
    parse_str(html_entity_decode(urldecode($str)), $arrParams);
    return $arrParams;
}

/**
 * 子孙树 用于菜单整理
 * @param $param
 * @param int $pid
 */
function subTree($param, $pid = 0)
{
    static $res = [];
    foreach($param as $key=>$vo){

        if( $pid == $vo['pid'] ){
            $res[] = $vo;
            subTree($param, $vo['id']);
        }
    }

    return $res;
}

/**
 * 整理菜单住方法
 * @param $param
 * @return array
 */
function prepareMenu($param)
{
    $param = objToArray($param);
    $parent = []; //父类
    $child = [];  //子类

    foreach($param as $key=>$vo){

        if(0 == $vo['type_id']){
            $vo['href'] = '#';
            $parent[] = $vo;
        }else{
            $vo['href'] = url($vo['control_name'] .'/'. $vo['action_name']); //跳转地址
            $child[] = $vo;
        }
    }

    foreach($parent as $key=>$vo){
        foreach($child as $k=>$v){

            if($v['type_id'] == $vo['id']){
                $parent[$key]['child'][] = $v;
            }
        }
    }
    unset($child);

    return $parent;
}

/**
 * 解析备份sql文件
 * @param $file
 */
function analysisSql($file)
{
    // sql文件包含的sql语句数组
    $sqls = array ();
    $f = fopen ( $file, "rb" );
    // 创建表缓冲变量
    $create = '';
    while ( ! feof ( $f ) ) {
        // 读取每一行sql
        $line = fgets ( $f );
        // 如果包含空白行，则跳过
        if (trim ( $line ) == '') {
            continue;
        }
        // 如果结尾包含';'(即为一个完整的sql语句，这里是插入语句)，并且不包含'ENGINE='(即创建表的最后一句)，
        if (! preg_match ( '/;/', $line, $match ) || preg_match ( '/ENGINE=/', $line, $match )) {
            // 将本次sql语句与创建表sql连接存起来
            $create .= $line;
            // 如果包含了创建表的最后一句
            if (preg_match ( '/ENGINE=/', $create, $match )) {
                // 则将其合并到sql数组
                $sqls [] = $create;
                // 清空当前，准备下一个表的创建
                $create = '';
            }
            // 跳过本次
            continue;
        }

        $sqls [] = $line;
    }
    fclose ( $f );

    return $sqls;
}

/**
 * 统一返回信息
 * @param $code
 * @param $data
 * @param $msge
 */
function msg($code, $data, $msg)
{
    return compact('code', 'data', 'msg');
}

/**
 * 对象转换成数组
 * @param $obj
 */
function objToArray($obj)
{
    return json_decode(json_encode($obj), true);
}

/**
 * 权限检测
 * @param $rule
 */
function authCheck($rule)
{
    $control = explode('/', $rule)['0'];
    if(in_array($control, ['login', 'index'])){
        return true;
    }

    if(in_array($rule, session('action'))){
        return true;
    }

    return false;
}

/**
 * 整理出tree数据 ---  layui tree
 * @param $pInfo
 * @param $spread
 */
function getTree($pInfo, $spread = true)
{

    $res = [];
    $tree = [];
    //整理数组
    foreach($pInfo as $key=>$vo){

        if($spread){
            $vo['spread'] = true;  //默认展开
        }
        $res[$vo['id']] = $vo;
        $res[$vo['id']]['children'] = [];
    }
    unset($pInfo);

    //查找子孙
    foreach($res as $key=>$vo){
        if(0 != $vo['pid']){
            $res[$vo['pid']]['children'][] = &$res[$key];
        }
    }

    //过滤杂质
    foreach( $res as $key=>$vo ){
        if(0 == $vo['pid']){
            $tree[] = $vo;
        }
    }
    unset( $res );

    return $tree;
}

function replace_weibo ($content) {


    $phiz = [
        'hehe' => '呵呵',
        'xixi' => '嘻嘻',
        'haha' => '哈哈',
        'keai' => '可爱',
        'kelian' => '可怜',
        'wabisi' => '挖鼻屎',
        'chijing' => '吃惊',
        'haixiu' => '害羞',
        'jiyan' => '挤眼',
        'bizui' => '闭嘴',
        'bishi' => '鄙视',
        'aini' => '爱你',
        'lei' => '泪',
        'touxiao' => '偷笑',
        'qinqin' => '亲亲',
        'shengbin' => '生病',
        'taikaixin' => '太开心',
        'ldln' => '懒得理你',
        'youhenhen' => '右哼哼',
        'zuohenhen' => '左哼哼',
        'xiu' => '嘘',
        'shuai' => '衰',
        'weiqu' => '委屈',
        'tu' => '吐',
        'dahaqian' => '打哈欠',
        'baobao' => '抱抱',
        'nu' => '怒',
        'yiwen' => '疑问',
        'canzui' => '馋嘴',
        'baibai' => '拜拜',
        'sikao' => '思考',
        'han' => '汗',
        'kun' => '困',
        'shuijiao' => '睡觉',
        'qian' => '钱',
        'shiwang' => '失望',
        'ku' => '酷',
        'huaxin' => '花心',
        'heng' => '哼',
        'guzhang' => '鼓掌',
        'yun' => '晕',
        'beishuang' => '悲伤',
        'zuakuang' => '抓狂',
        'heixian' => '黑线',
        'yinxian' => '阴险',
        'numa' => '怒骂',
        'xin' => '心',
        'shuangxin' => '伤心',
        'weixiao'=>'微笑',
        'huaixiao'=>'坏笑',
    ];


    $preg = '/\[(\S+?)\]/is';
    preg_match_all($preg, $content, $arr);
    if (!empty($arr[1])) {
        foreach ($arr[1] as $k => $v) {
            $name = array_search($v, $phiz);
            if ($name) {
                $content = str_replace($arr[0][$k], '<img src=" /static/index/images/phiz/' . $name . '.gif" title="' . $v . '"/>', $content);
            }
        }
    }

    $preg = '/\#(\S+?)\#/is';
    preg_match($preg, $content, $arr);
    if(!empty($arr[1])){
        $content = str_replace($arr[1],'<a  class="jump" data-name="'.$arr[1].'">'.$arr[1].'</a>',$content);
    }

    return $content;
}




